﻿using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;

namespace Exercise.MVC.Attrbute
{
    //授权特性
    public class CustomerAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            var context = filterContext.HttpContext;  //获取请求上下文
            //if()
            //filterContext.Result = new RedirectResult("");  //重定向 result 

            //filterContext.Result = new JsonResult();
            //throw new Exception("");
            //
            //filterContext.
            //判断方法是否存在该特性
            var de = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
            //HttpApplication application = new HttpApplication();
            return;  //继续执行
        }
    }

    public class CustomerHandlerAttribute : HandleErrorAttribute
    {
        //Customer 
        public override void OnException(ExceptionContext filterContext)
        {
            var fileterContent = filterContext.Exception;
            if (!filterContext.ExceptionHandled) //没有被别的HandleErrorAttribute处理
            {
                var d = filterContext.HttpContext.Request.Url.AbsoluteUri;
                var msg = filterContext.Exception.Message;
                //filterContext.Result = new ViewResult()
                //{
                //    ViewName = "~/Views/Shared/Error.cshtml",
                //    ViewData = new ViewDataDictionary<string>(msg)
                //};
            }
            //if (filterContext.HttpContext.Request.IsAjaxRequest())
            //{
            //    filterContext.Result = new JsonResult() { Data = msg };
            //}
            filterContext.ExceptionHandled = true;
        }

        /// <summary>
        /// 方法过滤器
        /// </summary>

    }


    public class CustomerActionFilterAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var t = filterContext.ActionParameters;
            Console.WriteLine("OnActionExecuting");
            //压缩数据
            //filterContext.HttpContext.Response.Filter = new GZipStream();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            Console.WriteLine("OnActionExecuted");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            Console.WriteLine("OnResultExecuting");
            filterContext.HttpContext.Response.Write("OnResultExecuting");

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            //Console.WriteLine("OnResultExecuted");
            //filterContext.HttpContext.Response.Write("OnResultExecuted");
        }
    }

    public class CustomerRtmpHandle : IHttpHandler
    {
        public bool IsReusable => true;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            context.Response.Write("This is AAA");
        }
    }

    /// <summary>
    /// 图片处理
    /// </summary>
    public class ImageHandle : IHttpHandler
    {
        bool IHttpHandler.IsReusable => true;

        //防盗链
        public void  ProcessRequest(HttpContext context)
        {
            if (context.Request.UrlReferrer == null || context.Request.UrlReferrer.Host == null)
            {
                //大部分都是爬虫
                context.Response.ContentType = "image/JPEG";
                context.Response.WriteFile("/Image/liuyan.webp");
            }
            else
            {
                //HttpApplication
                //如果UrlReferrer中不包含自己站点的主机域名，则显示一张默认的禁止盗链图片
                if (context.Request.UrlReferrer.Host.Contains("localhost"))
                {
                    string fileName = context.Server.MapPath(context.Request.FilePath);
                    context.Response.ContentType = "image/JPEG";
                    context.Response.WriteFile(fileName);

                }
                else 
                {
                    context.Response.ContentType = "image/JPEG";
                    context.Response.WriteFile("/Image/liuyan.webp");
                }
            }
        }
    }
}